/*
 * 表格工具
 * version : "1.1",
 * auther : "陈 庭",
 *
 * Date: 2010-11-24
 *
 */
(function($) {
	$.yxTable = {
		initCss : function(options){
			/*默认显示配置及用户当前配置*/
			var defaults = {

			}
			var config = $.extend(defaults,options);
			
			// 动态加入皮肤样式(CSS)
			$("script").each(function () { 
				if(this.src.toString().match(/jquery\.yxTable.*?js$/)) { 
					// 创建样式表
					$("head").append("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + this.src.toString().replace(/jquery\.yxTable.*?js$/, "") + "yxTable2.css" + "\" id=\"yxTableCss\" />");
					return false; 
				}
			});
		}
	}
	
	// 插件功能代码
    $.fn.yxTableRow = function(options) {
        var opts = $.extend({}, $.fn.yxTableRow.defaults, options);
        return this.each(function() {

			var thisTable= $(this); 
			var thisTabList = null;
			if($(thisTable).children('tbody').length > 0){
				thisTabList = $(thisTable).children('tbody').eq(0).children(opts.tagName);
			}else{
				thisTabList = $(thisTable).children(opts.tagName);
			}
			
			// 过滤行
			if(opts.endRow != 0){
				thisTabList=$(thisTabList).filter(":lt(" + opts.endRow + ")");
			}
			if(opts.startRow != 0){
				thisTabList=$(thisTabList).filter(":gt(" + (opts.startRow - 1) + ")");
			}

			//添加奇偶行颜色 
			$(thisTabList).filter(":even").addClass(opts.evenRowClass);
			$(thisTabList).filter(":odd").addClass(opts.oddRowClass);

			//添加活动行颜色 
			$(thisTabList).bind("mouseover",function(){ 
				$(this).addClass(opts.activeRowClass); 
			}).bind("mouseout",function(){ 
				$(this).removeClass(opts.activeRowClass); 
			});

			//添加点击颜色
			if(opts.clickAble){
				$(thisTabList).addClass('cursorHand');
				$(thisTabList).click(function(e){ 
					var eObj = e.target || window.event.srcElement;
					if(eObj.tagName.toUpperCase() == 'INPUT' && $(eObj).attr('type').toUpperCase() == 'CHECKBOX'){
						// do Nothing
					}else{
						if($(this).hasClass(opts.clickRowClass)){
							$(this).removeClass(opts.clickRowClass);  
						}else{
							if(!opts.multi){
								$(thisTabList).removeClass(opts.clickRowClass);
							}
							$(this).addClass(opts.clickRowClass); 
						}
						try{
							opts.clickEvent(this);
						}catch(e){}
					}
				}); 
			}
        });
    };

    //行数实例
    $.fn.yxTableRowPad = function(options) {
        var opts = $.extend({}, $.fn.yxTableRowPad.defaults, options);
        return this.each(function() {

			var thisTable= $(this);
			var tbody = null; 
			var thisTabList = null;
			var thisHeaderRow = null;
			var nullRowHtml = null;

			//取得内容列表
			if($(thisTable).children('tbody').length > 0){
				tbody = $(thisTable).children('tbody').eq(0);
				thisTabList = $(thisTable).children('tbody').eq(0).children(opts.tagName);
			}else{
				tbody = $(thisTable);
				thisTabList = $(thisTable).children(opts.tagName);
			}

			if($(thisTabList).length > 0){	//至少有一行内容
				var nullRowHtml = $(thisTabList).eq(0).clone();
				$(nullRowHtml).children('td').html('&nbsp;');
			}else{
				if($(thisTable).children('thead').length > 0){
					var nullRowHtml = $(thisTable).children('thead').eq(0).children('tr').eq(0).clone();	//表头第一行

					if($(nullRowHtml).children('th').length > 0){
						var colNum = $(nullRowHtml).children('th').length;

						$(nullRowHtml).html('');

						for(var i = 0; i < colNum; i++){
							$(nullRowHtml).append('<td>&nbsp;</td>');
						}
					}else{
						$(nullRowHtml).children('td').html('&nbsp;');
					}
				}
			}

			if(nullRowHtml != null){
				//取得内容列表
				for(var i = $(thisTabList).length; i < opts.rowNum; i++){
					$(tbody).append($(nullRowHtml).clone());
				}
			}
        });
    };
	
	// 去掉奇偶行样式
    $.fn.removeRowClass = function(options) {
        var opts = $.extend({}, $.fn.yxTableRow.defaults, options);
        return this.each(function() {
			var thisTable= $(this); 
			var thisTabList = null;
			if($(thisTable).children('tbody').length > 0){
				thisTabList = $(thisTable).children('tbody').eq(0).children(opts.tagName);
			}else{
				thisTabList = $(thisTable).children(opts.tagName);
			}

			// 过滤行
			if(opts.endRow != 0){
				thisTabList=$(thisTabList).filter(":lt(" + opts.endRow + ")");
			}
			if(opts.startRow != 0){
				thisTabList=$(thisTabList).filter(":gt(" + opts.startRow + ")");
			}

			//添加奇偶行颜色 
			$(thisTabList).removeClass(opts.evenRowClass);
			$(thisTabList).removeClass(opts.oddRowClass);
			$(thisTabList).unbind("mouseover");
			$(thisTabList).unbind("mouseout");
			$(thisTabList).unbind("click");
        });
    };

	// 默认配置参数
    $.fn.yxTableRow.defaults = {
		tagName: "tr", // 行的标记
		startRow: 0,   // 区域开始行
		endRow: 0,     // 区域结束行
		clickAble: false,   // 是否可以点击
		multiClick: false,  // 是否可以多次点击
		clickEvent : function(thisRow){},  // 点击产生事件
		evenRowClass:"evenRow",   // 偶数行样式
		oddRowClass:"oddRow",     // 奇数行样式
		activeRowClass:"activeRow",  // 鼠标经过样式
		clickRowClass:"clickRow"     // 点击后样式
    };

    $.fn.yxTableRowPad.defaults = {
		rowNum: 0,   // 总行数
    };

})(jQuery);